package com.niuwa.p2p.core.finance.repay;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.niuwa.p2p.core.finance.CostCalculatorHelper;
import com.niuwa.p2p.core.utils.DateUtil;
import com.niuwa.p2p.entity.business.RepayPlan;

/**
 * 还款 
 * 实现getRepayShould,算出当期应该还款(提前还款) 
 * 
 * @title Repay.java
 *
 * @author 计盛君
 * @version 1.0
 * @created 2015年4月29日
 */
public class RepayEarlier extends Repay {
	/** 
	 * 根据已经存入DB的当期还款信息和还款日，结合借款信息（合同本金，借款方式等）计算费率，算出应还金额
	 * 
	 * 提前还款
	 *   逾期时提前还款：    提前还款手续费（平台） + 逾期清偿  + 正常清偿 + 未到期部分待还本金
	 *   未逾期时提前还款：提前还款手续费（平台）+ 正常清偿  + 未到期部分待还本金
	 *  
	 * 
	 * @param loanInfo 借款信息
	 * @param RepayPlan   当期还款信息
	 * @param repaymentDay 还款日
	 * @return 还款金额
	 * @throws Exception 
	 */
	@Override
	public BigDecimal getRepayShould(LoanInfoVo loanInfoVo, RepayPlan RepayPlan, Date repaymentDay, IRepayCallback repayCallback) throws Exception {
		// 提前还款费用 = 提前还款手续费（平台）
		BigDecimal repayEarlierAmout = CostCalculatorHelper.repayEarlierFee(loanInfoVo.getRepayTotalAmount(), loanInfoVo.getRepayEarlierFeeRate());
		List<com.niuwa.p2p.entity.business.RepayPlan> repayPlanDB = repayCallback.getRepayPlanDB();
		// 提前还款费用 = 提前还款手续费（平台）+ 剩余费用(逾期清偿  + 正常清偿 + 未到期部分待还本金)
		repayEarlierAmout = repayEarlierAmout.add(getRepayEarlierRemainAmout(repayPlanDB, loanInfoVo, RepayPlan, repaymentDay, repayCallback));				
		return repayEarlierAmout;
	}
	
	/**
	 * 
	 * @param repayPlanDB 入库的还款计划信息
	 * @param loanInfo 借款信息
	 * @param RepayPlan   当期还款信息
	 * @param repaymentDay 还款日
	 * @return
	 * @throws Exception
	 */
	private BigDecimal getRepayEarlierRemainAmout(List<com.niuwa.p2p.entity.business.RepayPlan> repayPlanDB, LoanInfoVo loanInfoVo, RepayPlan RepayPlan, Date repaymentDay, IRepayCallback repayCallback) throws Exception{
		IPay<LoanInfoVo, RepayPlan> repay = new Repay();
		
		BigDecimal repayEarlierRemainAmout = BigDecimal.ZERO;
		for(int i = 1; i<= repayPlanDB.size(); i++) {
			com.niuwa.p2p.entity.business.RepayPlan currentRepayPlan = repayPlanDB.get(i);
			BigDecimal currentRepayShouldRemainAmout = getCurrentRepayShouldRemainAmout(repayPlanDB.get(i));
			// 当期偿清结束，不计入费用， 没有偿清结束的，计算费用
			if(currentRepayShouldRemainAmout.compareTo(BigDecimal.ZERO) == 1) {	
				//  实际还款日(repaymentDay) >  计划还款日(repayDate) 计算 逾期还款或者正常还款				
				if(currentRepayPlan.getTermNo() == 1 || currentRepayPlan.getTermNo() == repayPlanDB.size()){
				//  第一期或者最后一期 作为正常还款
					repayEarlierRemainAmout = repayEarlierRemainAmout.add(repay.getRepayShould(loanInfoVo, RepayPlan, repaymentDay, repayCallback));
				} else if(currentRepayPlan.getTermNo() > 1 && DateUtil.isAfter(repaymentDay, repayPlanDB.get(i+1).getRepayDate())) {
				// 不是第一期   && 实际还款日 > 下期计划还款日   作为 逾期计算
				// 当 逾期没有还清的时候,再次计算逾期费用(>DB中的逾期费用  既：昨天计算得到的逾期费用)
				// 偿还金额 = 当天的逾期费用 - 已经偿还的逾期费用
					repayEarlierRemainAmout = repayEarlierRemainAmout.add(currentRepayShouldRemainAmout);			
				} else if (DateUtil.compare(repaymentDay, repayPlanDB.get(i).getRepayDate()) > 0 && DateUtil.compare(repaymentDay, repayPlanDB.get(i+1).getRepayDate()) <= 0) {
				// 当期计划还款日   < 实际还款日 < 下期计划还款日  作为正常还款
					repayEarlierRemainAmout = repayEarlierRemainAmout.add(repay.getRepayShould(loanInfoVo, RepayPlan, repaymentDay, repayCallback));	
				} else {
				// 实际还款日(repaymentDay) <=  计划还款日(repayDate) 计算 未到期部分待还本金
					// 当期提前还款金额 = 当期提前还款金额 + 还款本金
					repayEarlierRemainAmout = repayEarlierRemainAmout.add(repayPlanDB.get(i).getRepayCapital());
				}				
			}
			
		}
		return repayEarlierRemainAmout;
	}
		
	
}
